#include "head.h"

int main() {
    int flag;
    int socket_fd;
    sendMsg msg;
    client_status user;
    c_sends sends;
    char token[2000];
    memset(token,0,sizeof(token));
    //建立连接 socket 拿到fd
    //socket_fd = socket(AF_INET, SOCK_STREAM, 0);

    // 建立连接
    do {

        printf("------------------------------\n"); 
        printf("|******欢迎进入网盘系统******|\n");
        printf("|********  1、登录  *********|\n");
        printf("|********  2、注册  *********|\n");
        // printf("|********  3、退出  *********|\n");
        printf("|****************************|\n");
        printf("------------------------------\n"); 
        printf("请输入您的选择：");
        fflush(stdout);
        char status[1024] = { 0 };
        int read_ret = read(STDIN_FILENO, status, sizeof(status));

        printf("read_ret = %d\n", read_ret);
        status[strlen(status) - 1] = '\0';
        //printf("%s\n", status);
        if (strcmp(status, "1") == 0) {
            sends.isstatus = 1;
        }
        else if (strcmp(status, "2") == 0) {
            sends.isstatus = 0;
        }
        else{
            printf("请重新输入.\n");
            flag=true;
            continue;
        }
        flag = login(&socket_fd, &user, sends,token);
        printf("islogin:%d\n",user.islogin);

        //
        if(strcmp(token,"0")!=0){
            strcpy(msg.token,token);
        }else{
            printf("token未获取.\n");
            flag=true;
            continue;
        }

    } while (flag);


    printf(" id: %sname :%spassword: %s\n", user.user_id, user.user_name, user.password);
    printf("--------------------------\n"); 
    printf("|******可输入的命令******|\n");
    printf("|********  ls  **********|\n");
    printf("|********  rm  **********|\n");
    printf("|********  cd  **********|\n");
    printf("|********  pwd **********|\n");
    printf("|********  puts *********|\n");
    printf("|********  gets *********|\n");
    printf("|********  mkdir ********|\n");
    printf("|********  exit *********|\n");
    printf("--------------------------\n"); 

    //创建处理gets、puts的线程
    pthread_info *p_info;
    //bzero(p_info,sizeof(pthread_info));
    initStatus(&p_info);
    p_info->is_gets=false;
    //把user的内容放p_info
    memcpy(&p_info->consumer,&user,sizeof(user));
    strcpy(p_info->send_info.token,token);

    while (1) {
        printf("%s:%s$ ",user.user_name,user.path);
        fflush(stdout);
        char bufs[1024];

        bzero(bufs, sizeof(bufs));
        int read_ret = read(STDIN_FILENO, bufs, sizeof(bufs));
        ERROR_CHECK(read_ret, -1, "read_ret");
        //删除回车
        int len = strlen(bufs);
        len = len - 1;
        bufs[len] = '\0';
        //解剖 命令 空格切开
        char* savebuf = { 0 };
        char* token = strtok_r(bufs, " ", &savebuf);

        //保存 后续切割路径
        strcpy(user.path_last, savebuf);

        strcpy(p_info->consumer.path,user.path);
        strcpy(p_info->consumer.path_last,savebuf);

        //  printf("name = %s,  path = %s\n", user.user_name, user.path);
        if (strcmp(token, "ls") == 0) {

            my_ls(msg, user, socket_fd);
        }
        else if (strcmp(token, "cd") == 0) {

            my_cd(msg, &user, socket_fd);

        }
        else if (strcmp(token, "rm") == 0) {

            my_rm(msg, user, socket_fd);

        }
        else if (strcmp(token, "puts") == 0 || strcmp(token, "gets") == 0) {

            if(strcmp(token,"gets")==0){
                p_info->is_gets=true;
            }
            pthread_cond_signal(&p_info->one.cond);
            continue;
            //处理puts的逻辑
            //处理长命令的函数
            //  handle_long_commmand(msg,user,is_gets);
            //         my_puts(msg, user, socket_fd);

        }
        // else if (strcmp(token, "gets") == 0) {

        //       my_gets(msg, user, socket_fd);

        //  }
        else if (strcmp(token, "pwd") == 0) {

            my_pwd(msg, user, socket_fd);
        } else if(strcmp(token, "mkdir")==0){
            my_mkdir(msg, user, socket_fd);
        }
        else if(strcmp(token,"exit")==0){
            if(unlogin(msg,&user,socket_fd)){
                printf("用户正常退出\n");
                break;        
            }

        } else {
            printf("错误命令！！\n");
        }
    }
    return 0;
}
